﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class DeptController : Controller
    {
	      private DeptServers deptServers;

		    public DeptController()
			{
				deptServers = new DeptServers();
			}


		// GET: System/Dept
        public ActionResult Index()
        {
            ViewBag.MANAGERData = JsonConvert.SerializeObject(deptServers.GetMANAGER());
            return View();
        }

        public ActionResult Details(int? id)
        {
            ViewBag.DEPT_PARENTData = deptServers.GetDEPT_PARENT();
            var model = new department();
            ViewBag.Title = "部门明细";
            if (id != null)
            {               
                model = GetModel(id.Value.ToString());
            }
            return View(model);
        }
        // GET: System/Dept/Edit/5
        public ActionResult Edit(int? id)
        {
			ViewBag.DEPT_PARENTData = deptServers.GetDEPT_PARENT();
            var model = new department();
            if (id != null)
            {
                ViewBag.Title = "编辑部门";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建部门";
            }
            return View(model);
        }

		// POST: System/Dept/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, department model)
        {
			ViewBag.DEPT_PARENTData = deptServers.GetDEPT_PARENT();
            try
            {
                if (id == null || id == 0)
                {
                    deptServers.Add(model);
                    ViewBag.msg = "新增部门信息成功！";
                    model = new department();

                }
                else
                {
                    deptServers.Update(model);
                    ViewBag.msg = "修改部门信息成功！";
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

		// GET: System/Dept/Delete/5
        public ActionResult Delete(int id)
        {
		    if (id != 0)
            {
                deptServers.Del(id.ToString());
                ViewBag.msg = "删除部门信息成功！";                   
            }    
            return Content("");
        }


		 #region //GetData
		public string GetTableJsonResult(string DEPT_NAME,string MANAGER,string G_DEPT)
        {
            var list = deptServers.GetData(DEPT_NAME, MANAGER, G_DEPT);
            return JsonConvert.SerializeObject(list);
        }

		public department GetModel(string id)
        {
            var model = deptServers.GetModelById(id);
            return model;
        }

		 #endregion //GetData
	}
}